/**
 * @file   PacketHeader.h
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-03-10
 * @brief  DataBus Packet Header.
 *
 * Copyright (C) 2013  Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBDATABUS_COMMON_PACKETHEADER_H
#define LIBDATABUS_COMMON_PACKETHEADER_H

#include <DataBus/Common/Common.h>

namespace DataBus
{
/**
 * @brief The PacketHeader class
 */
class DATABUSSHARED_EXPORT PacketHeader
{
public:
    /**
     * @brief Constructor for PacketHeader
     */
    PacketHeader();

    /**
     * @brief Constructor for PacketHeader
     *
     * @param source        Source
     * @param destination   Destination
     * @param packetType    Packet type
     * @param packetId      Packet ID
     * @param payloadSize   Payload Size
     */
    PacketHeader(const quint8 source,
                 const quint8 destination,
                 const PacketType packetType,
                 const quint8 packetId,
                 const quint8 payloadSize);

    /**
     * @brief Reads the source of the communication (Client ID)
     *
     * @return Source Client ID
     */
    quint8 getSource() const;

    /**
     * @brief Sets the source of the communication (Client ID)
     *
     * @param source    Source Client ID
     */
    void setSource(const quint8 source);

    /**
     * @brief Reads the destination of the communication (Client ID)
     *
     * @return Destination Client ID
     */
    quint8 getDestination() const;

    /**
     * @brief Sets the destination of the communication (Client ID)
     *
     * @param destination   Destination Client ID
     */
    void setDestination(const quint8 destination);

    /**
     * @brief Reads the packet type
     *
     * @return Packet type
     */
    PacketType getPacketType() const;

    /**
     * @brief Sets the packet type
     *
     * @oaram packetType    Packet type
     */
    void setPacketType(const PacketType packetType);

    /**
     * @brief Reads the packet ID
     *
     * @return Packet ID
     */
    quint8 getPacketId() const;

    /**
     * @brief Set the packet ID
     *
     * @param packetId  Packet ID
     */
    void setPacketId(const quint8 packetId);

    /**
     * @brief Reads the payload size
     *
     * @return Payload size in bytes
     */
    quint8 getPayloadSize() const;

    /**
     * @brief Set the payload size
     *
     * @param payloadSize   Payload size in bytes
     */
    void setPayloadSize(const quint8 payloadSize);

    /**
     * @brief Convert packet header to byte array
     *
     * @return Byte array with packet header
     */
    QByteArray toBinary() const;

    /**
     * @brief Parse data for a valid header
     *
     * @param packetHeaderData  Input data
     * @param startIndex        Start of header in input data
     *
     * @param[out] packetHeader Output object
     *
     * @retval true     Successfully parsed the packet header
     * @retval false    Failed to parse the packet header
     *
     */
    static bool parse(const QByteArray &packetHeaderData,
                      const int startIndex,
                      PacketHeader *packetHeader);

private:
    /**
     * @brief Source of the communication (Client ID)
     */
    quint8 m_source;

    /**
     * @brief Destination of the communication (Client ID)
     */
    quint8 m_destination;

    /**
     * @brief Packet type
     */
    PacketType m_packetType;

    /**
     * @brief Packet unique indentification
     */
    quint8 m_packetId;

    /**
     * @brief Packet payload size
     */
    quint8 m_payloadSize;
};
}

#endif // LIBDATABUS_COMMON_PACKETHEADER_H
